package com.xingu.xg.controller;

import com.xingu.xg.common.consts.ComRT;
import com.xingu.xg.common.consts.ComResult;
import com.xingu.xg.dal.domain.Category1;
import com.xingu.xg.dal.dto.Category1TreeNode;
import com.xingu.xg.dal.vo.Category1FormVo;
import com.xingu.xg.dal.vo.category.req.AddNodeQuery;
import com.xingu.xg.dal.vo.category.req.CopyNodeQuery;
import com.xingu.xg.dal.vo.category.req.DeleteNodeQuery;
import com.xingu.xg.service.Category1Service;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Validated
@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    Category1Service category1Service;

    @ApiOperation("树形列表")
    @GetMapping("/list")
    public ComResult<List<Category1TreeNode>> list(){

        List<Category1TreeNode> category1TreeNodes = category1Service.selectTree();
        return ComRT.success(category1TreeNodes);
    }

    @ApiOperation("新增树节点")
    @PostMapping("/create")
    public ComResult<Category1> create(@RequestBody Category1FormVo req){
        ComResult<Category1> res = category1Service.create(req);
        return res;
    }

    @ApiOperation("自动创建同层不同顺序")
    @PostMapping("/create/sameLevel")
    public ComResult<Category1> createSameLevel( @Valid @RequestBody AddNodeQuery  req ){
        Long curNodeId = Long.valueOf(req.getSelectedId());
        ComResult res = category1Service.createSameLevel(curNodeId);
        return res;
    }

    @ApiOperation("自动创建上下层")
    @PostMapping("/create/fatherAndSub")
    public ComResult<Category1> createFatherAndSub( @Valid @RequestBody AddNodeQuery  req ){
        Long curNodeId = Long.valueOf(req.getSelectedId());

        ComResult res = category1Service.createFatherAndSub(curNodeId);
        return res;
    }




    @ApiOperation("复制子树节点到指定的层")
    @PostMapping("/copy")
    public ComResult<Category1> copyNode(@Valid @RequestBody CopyNodeQuery req){

        ComResult<Category1> res = category1Service.copyNodeFromId(req);
        return res;
    }



    @ApiOperation("级联删除全部的子树结构")
    @DeleteMapping("/delete")
    public ComResult<Category1> delete(@RequestBody DeleteNodeQuery req){
        List<Long> deleteList = req.getDeleteList();

        ComResult<Category1> res = category1Service.deleteBatch(deleteList);
        return res;
    }

}